package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import javax.xml.datatype.DatatypeFactory;  
import javax.xml.datatype.XMLGregorianCalendar;  
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar;

import com.lb.jwitter.service.ConditionReport; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.util.Message; 
import java.util.Collections; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import java.util.List; 
import java.util.Map; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.lb.jwitter.service.TblDKMHDotDKNhom;
import com.lb.jwitter.service.TblDKMHDotDKNhomService;

import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKNhomAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private String idList; 
    public String getIdList() { return idList; } 
    public void setIdList(String idList) { this.idList = idList; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public Long getId() {
       return id;
 }

    public void setId(Long id) {
       this.id = id;
 }

    private String ma; 
    public void setMa(String ma) {
       this.ma = ma;
 }

    public String getMa() {
       return ma;
 }

    private String ten; 
    public void setTen(String ten) {
       this.ten = ten;
 }

    public String getTen() {
       return ten;
 }

    private int dotDKId; 
    public void setDotDKId(int dotDKId) {
       this.dotDKId = dotDKId;
 }

    public int getDotDKId() {
       return dotDKId;
 }

    private String ngayBatDau; 
    public void setNgayBatDau(String ngayBatDau) {
       this.ngayBatDau = ngayBatDau;
 }

    public String getNgayBatDau() {
       return ngayBatDau;
 }

    private String ngayKetThuc; 
    public void setNgayKetThuc(String ngayKetThuc) {
       this.ngayKetThuc = ngayKetThuc;
 }

    public String getNgayKetThuc() {
       return ngayKetThuc;
 }

    private String quyen; 
    public void setQuyen(String quyen) {
       this.quyen = quyen;
 }

    public String getQuyen() {
       return quyen;
 }

    private TblDKMHDotDKNhom tblDKMHDotDKNhom; 
    public TblDKMHDotDKNhom getTblDKMHDotDKNhom () {
        return this.tblDKMHDotDKNhom ;
 }

    private Collection<TblDKMHDotDKNhom> tblDKMHDotDKNhoms; 
    public Collection<TblDKMHDotDKNhom> getTblDKMHDotDKNhoms() {
        return this.tblDKMHDotDKNhoms; 
      } 

    private TblDKMHDotDKNhomService tblDKMHDotDKNhomService; 

    private Collection<TblDKMHDotDK> tblDKMHDotDKs; 
    public Collection<TblDKMHDotDK> getTblDKMHDotDKs() {
         return this.tblDKMHDotDKs; 
    }

    private TblDKMHDotDKService tblDKMHDotDKService; 

	private int preUpdate(Message msg, TblDKMHDotDKNhom obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblDKMHDotDKNhom obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String update() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "sua")){ 
        return "noprivilege"; 
    } 
        tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService)context.getBean("tbl_DKMH_DotDK_NhomClient"); 
        msg = new Message(); 
        tblDKMHDotDKNhom = tblDKMHDotDKNhomService.gettblDKMHDotDKNhom(new Long(id)); 
        tblDKMHDotDKNhom.setMa(ma); 
        tblDKMHDotDKNhom.setTen(ten); 
//        tblDKMHDotDKNhom.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId))); 
        
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayBatDau); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKNhom.setNgayBatDau(d); 
        
       dd = df.parse(ngayKetThuc); 
       c.setTime(dd);
       d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKNhom.setNgayKetThuc(d); 
        tblDKMHDotDKNhom.setQuyen(quyen); 
        preUpdate(msg,tblDKMHDotDKNhom); 
       if (msg.isPreMessage_b()) 
       { 
           tblDKMHDotDKNhomService.updatetblDKMHDotDKNhom(tblDKMHDotDKNhom); 
           
           tblDKMHDotDKNhomService.calSPUpdateChiTietHVDangKyTheoNhomDK(tblDKMHDotDKNhom.getId());
           postUpdate(msg, tblDKMHDotDKNhom); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblDKMHDotDKNhoms =       tblDKMHDotDKNhomService.findAlltblDKMHDotDKNhom(pageSize, page); 
      pages = tblDKMHDotDKNhomService.counttblDKMHDotDKNhomByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ma="; 
      dataString += "&ten="; 
      dataString += "&dotDKId="; 
      dataString += "&ngayBatDau="; 
      dataString += "&ngayKetThuc="; 
      dataString += "&quyen="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_DKMH_DotDK_Nhom Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "xem")){ 
        return "noprivilege"; 
    } 
           tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService)context.getBean("tbl_DKMH_DotDK_NhomClient"); 
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        tblDKMHDotDKNhoms =        tblDKMHDotDKNhomService.findAlltblDKMHDotDKNhom(pageSize, page); 
        pages = tblDKMHDotDKNhomService.counttblDKMHDotDKNhomByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ma="; 
      dataString += "&ten="; 
      dataString += "&dotDKId="; 
      dataString += "&ngayBatDau="; 
      dataString += "&ngayKetThuc="; 
      dataString += "&quyen="; 
           tblDKMHDotDKService = (TblDKMHDotDKService)context.getBean("tbl_DKMH_DotDKClient"); 
      tblDKMHDotDKs = tblDKMHDotDKService.findAlltblDKMHDotDK(0, 0);
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_DKMH_DotDK_Nhom Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
  public String delete() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "xoa")){ 
        return "noprivilege"; 
    }           
    tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService)context.getBean("tbl_DKMH_DotDK_NhomClient"); 
    msg = new Message(); 

    List<String> ids = new ArrayList<String>(); 
    Collections.addAll(ids, idList.split(",")); 
    for(String id : ids){ 

    	Long nhomId = new Long(id.trim());
    	if(tblDKMHDotDKNhomService.calSPDeleteChiTietHVDangKyTheoNhomDK(nhomId)){
    		tblDKMHDotDKNhomService.deletetblDKMHDotDKNhom(nhomId);
    	}
    } 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblDKMHDotDKNhoms =       tblDKMHDotDKNhomService.findAlltblDKMHDotDKNhom(pageSize, page); 
      pages = tblDKMHDotDKNhomService.counttblDKMHDotDKNhomByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ma="; 
      dataString += "&ten="; 
      dataString += "&dotDKId="; 
      dataString += "&ngayBatDau="; 
      dataString += "&ngayKetThuc="; 
      dataString += "&quyen="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Delete tbl_DKMH_DotDK_Nhom Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 	private int preInsert(Message msg, TblDKMHDotDKNhom obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblDKMHDotDKNhom obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
	
	
	
	
	private int tuKhoaHoc;
	private int denKhoaHoc;
	private int tuSoTCNo;
	private int denSoTCNo;
	
 public int getTuKhoaHoc() {
		return tuKhoaHoc;
	}

	public void setTuKhoaHoc(int tuKhoaHoc) {
		this.tuKhoaHoc = tuKhoaHoc;
	}

	public int getDenKhoaHoc() {
		return denKhoaHoc;
	}

	public void setDenKhoaHoc(int denKhoaHoc) {
		this.denKhoaHoc = denKhoaHoc;
	}

	public int getTuSoTCNo() {
		return tuSoTCNo;
	}

	public void setTuSoTCNo(int tuSoTCNo) {
		this.tuSoTCNo = tuSoTCNo;
	}

	public int getDenSoTCNo() {
		return denSoTCNo;
	}

	public void setDenSoTCNo(int denSoTCNo) {
		this.denSoTCNo = denSoTCNo;
	}

public String insert() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "them")){ 
        return "noprivilege"; 
    }           
    tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService)context.getBean("tbl_DKMH_DotDK_NhomClient"); 
           
    tblDKMHDotDKService = (TblDKMHDotDKService)context.getBean("tbl_DKMH_DotDKClient"); 
    
        msg = new Message(); 
        
        tblDKMHDotDKNhom = new TblDKMHDotDKNhom(); 
        
		String w = "-";
		String u = "-";
		String d = "-";
		if(quyen != null){
	        for(String s : quyen.split(",")){
	
	    		if(s.trim().equals("w"))
	    			w = "w";
	    		if(s.trim().equals("u"))
	    			u = "u";
	    		if(s.trim().equals("d"))
	    			d = "d";
	    	}
    	}
        
        quyen = w + u + d;;
        tblDKMHDotDKNhom.setMa(ma); 
        tblDKMHDotDKNhom.setTen(ten); 
        tblDKMHDotDKNhom.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId))); 
        
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayBatDau); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar xd = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKNhom.setNgayBatDau(xd); 
        
        
       dd = df.parse(ngayKetThuc); 
       c.setTime(dd);
       xd = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKNhom.setNgayKetThuc(xd); 
        tblDKMHDotDKNhom.setQuyen(quyen); 
        preInsert(msg,tblDKMHDotDKNhom); 
       if (msg.isPreMessage_b()) 
       { 
    	   //tao nhom dk
           id = tblDKMHDotDKNhomService.createtblDKMHDotDKNhom(tblDKMHDotDKNhom); 
           //tao chi tiet hoc vien dang ky cho nhom
           tblDKMHDotDKNhomService.calSPCreateChiTietHVDangKyTheoNhomDK(new Long(dotDKId), id, tuKhoaHoc, denKhoaHoc, tuSoTCNo, denSoTCNo);
           
           postInsert(msg, tblDKMHDotDKNhom); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblDKMHDotDKNhom.setId(id); 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblDKMHDotDKNhoms =       tblDKMHDotDKNhomService.findAlltblDKMHDotDKNhom(pageSize, page); 
      pages = tblDKMHDotDKNhomService.counttblDKMHDotDKNhomByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ma="; 
      dataString += "&ten="; 
      dataString += "&dotDKId="; 
      dataString += "&ngayBatDau="; 
      dataString += "&ngayKetThuc="; 
      dataString += "&quyen="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_DKMH_DotDK_Nhom Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String search() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "xem")){ 
        return "noprivilege"; 
    } 
           tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService)context.getBean("tbl_DKMH_DotDK_NhomClient"); 
        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(id!= null && !id.equals(0)){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("id");
        con.setValue1(new Long(id));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(ma != null && !ma.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ma");
        con.setValue1("%" + ma + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(ten != null && !ten.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ten");
        con.setValue1("%" + ten + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(dotDKId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("dotDKId.id");
        con.setValue1(new Long(dotDKId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(ngayBatDau != null && !ngayBatDau.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ngayBatDau");
        con.setValue1(ngayBatDau);
        con.setOperator("eq");
        con.setType("date");
        cons.add(con);	
    } 
    if(ngayKetThuc != null && !ngayKetThuc.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ngayKetThuc");
        con.setValue1(ngayKetThuc);
        con.setOperator("eq");
        con.setType("date");
        cons.add(con);	
    } 
    if(quyen != null && !quyen.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("quyen");
        con.setValue1("%" + quyen + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblDKMHDotDKNhoms = tblDKMHDotDKNhomService.findtblDKMHDotDKNhomByCondition(cons,pageSize,page); 
           pages = tblDKMHDotDKNhomService.counttblDKMHDotDKNhomByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&id=" + id; 
      dataString += "&ma=" + ma; 
      dataString += "&ten=" + ten; 
      dataString += "&dotDKId=" + dotDKId; 
      dataString += "&ngayBatDau=" + ngayBatDau; 
      dataString += "&ngayKetThuc=" + ngayKetThuc; 
      dataString += "&quyen=" + quyen; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_DKMH_DotDK_Nhom Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String editForm() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "sua")){ 
        return "noprivilege"; 
    } 
           tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService)context.getBean("tbl_DKMH_DotDK_NhomClient"); 
        tblDKMHDotDKNhom = tblDKMHDotDKNhomService.gettblDKMHDotDKNhom(new Long(id)); 
           tblDKMHDotDKService = (TblDKMHDotDKService)context.getBean("tbl_DKMH_DotDKClient"); 
        tblDKMHDotDKs = tblDKMHDotDKService.findAlltblDKMHDotDK(0, 0);

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_DKMH_DotDK_Nhom Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 } 
 
